package com.ldfile.server.controller;

import com.alibaba.fastjson.JSONObject;
import com.ldfile.server.dto.PdfExportDTO;
import com.ldfile.server.utils.MailUtil;
import com.ldfile.server.utils.PdfUtil;
import com.ldfile.server.utils.QRCodeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;

@Controller
@RequestMapping("/qrcode")
public class QRCodeController {

    @GetMapping("/createQrcode")
    public void createQrcode(@RequestParam("content") String content,
                             @RequestParam("title") String title,
                             @RequestParam("width") int width,
                             @RequestParam("height") int height,
                             @RequestParam(value = "level", required = false) Integer level,
                             @RequestParam(value = "logo", required = false) String logo,
                             HttpServletResponse response) {
        try {
            if (level == null || level < 0 || level > 3) {
                level = 0;
            }
            OutputStream outputStream = response.getOutputStream();
            InputStream in =null;
            if(!StringUtils.isEmpty(logo)){
                in=new FileInputStream(logo);
            }
            QRCodeUtil.zxingCodeCreate(content, title, width, height, outputStream, "jpg", level, in);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/createPdf")
    public void createCoverage(@RequestParam("param")String param,
                               HttpServletResponse response) throws Exception {
        PdfExportDTO pdfExportDTO= JSONObject.parseObject(param,PdfExportDTO.class);
        if (StringUtils.isEmpty(pdfExportDTO.getEmail())) {//下载为pdf
            response.setContentType("application/octet-stream");
            String filename = String.format("%s_%s.pdf", pdfExportDTO.getBatchCode(), LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            //2.设置文件头：最后一个参数是设置下载文件名(假如我们叫a.pdf)
            response.setHeader("Content-Disposition", String.format("attachment;fileName=%s", filename));
            OutputStream outputStream = response.getOutputStream();
            PdfUtil.createPdf(pdfExportDTO.getQrcodeList(), outputStream);
        } else {//发送邮件
            ByteArrayOutputStream bytsos = new ByteArrayOutputStream(1024 * 1024);
            PdfUtil.createPdf(pdfExportDTO.getQrcodeList(), bytsos);
            ByteArrayInputStream bis = new ByteArrayInputStream(bytsos.toByteArray());
            String filename = String.format("%s_%s.pdf", pdfExportDTO.getBatchCode(), LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            String subject = String.format("%s_%s_%s的追溯二维码", pdfExportDTO.getQrcodeList().get(0).getProName(), pdfExportDTO.getBatchCode(), LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            MailUtil.sendMail(Arrays.asList(pdfExportDTO.getEmail()), bis, filename, pdfExportDTO.getName(), subject);
        }

    }
}
